RecyclerView是个比较蛋疼的控件,给我的第一感觉就是竟然连OnItemClick都没有,
当然,网上有很多的解决方法,可以自己实现。
然而今天,在用到这个控件时,又遇到了个奇葩的问题,
使用过程中,发现 scrollToPosition() 和 smoothScrollToPosition() 都无效,
翻了下熊掌古狗,在stackoverflow看到很多也有遇到这种情况:
http://stackoverflow.com/questions/26580723/how-to-scroll-to-the-bottom-of-a-recyclerview-scrolltoposition-doesnt-work
http://stackoverflow.com/questions/27819507/recyclerview-scrolltoposition-not-trigger-scrolllistener
http://stackoverflow.com/questions/30845742/smoothscrolltoposition-doesnt-work-properly-with-recyclerview
在最后一篇,终于找到了能用的方法,scrollToPositionWithOffset(int, int),但效果不是很理想,没有平滑滚动,而是直接生硬地移到了指定位置。
mLayoutManager = mRecyclerView.getLayoutManager();
mLayoutManager.scrollToPositionWithOffset(pos, 0);
更新:
今天找到原因,没有滚动是因为 要滚动到的位置,已经在屏幕里面了,这时候是不滚动的,
只有要滚到的位置没有在屏幕上,才会滚动。
解决方法可以参考这里:
http://blog.csdn.net/tyzlmjj/article/details/49227601